package top.superflow.core.pool.defaults

import top.superflow.core.housekeep.HouseKeeper
import top.superflow.core.pool.config.PoolConfig
import top.superflow.log.Loggable

import scala.beans.BeanProperty
import scala.collection.mutable.ListBuffer

trait AbstractHouseKeeper[C <: PoolConfig,T]
        extends HouseKeeper with Loggable{

  @BeanProperty
  var idleTimeout : Int
  
  val idleCleaner = {
    val timeoutIdles = ListBuffer[T]()
    
    for(element <- getIdlesResources if this.isIdleTimeout(element)){
      timeoutIdles += element
    }
    
    for(element <- timeoutIdles){
      this.evict(element)
    }
  }

  def getIdlesResources : List[T]

  def isIdleTimeout(t : T) : Boolean

  def evict(t : T)
  
  def scheduleCleaner() {

    if(this.getIdleTimeout > 0 && this.getIdleTimeout < Long.MaxValue){
      this.schedule(this.getIdleTimeout / (1000 * 2), idleCleaner)
    }
  }


}